﻿using System;
using System.Collections.Generic;
using LogSystem;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.Liberty.Pool
{
    public class LibertyPoolMgr : SingletonMono<LibertyPoolMgr>
    {
        private Dictionary<int, EffectPrefabPool> effectPoolMap = new Dictionary<int, EffectPrefabPool>(64);
        private Dictionary<int, Transform> showingEffectMap = new Dictionary<int, Transform>(64);
        private Dictionary<int, int> unique2ConfigMap = new Dictionary<int, int>(64);
        

        private Transform effectRoot;

        public Transform EffectRoot
        {
            get
            {
                if (effectRoot == null)
                {
                    effectRoot = new GameObject("effectRoot").transform;
                    effectRoot.parent = this.transform;
                }

                return effectRoot;
            }
        }
        
        // 通用对象池
        private HashSet<string> _PoolNames = new HashSet<string>();

        private void OnDestroy()
        {
            // 卸掉对象池资源
            foreach (string path in _PoolNames)
            {
                GameObjectPoolManager.GetInstance()?.Destroy(path);
            }
            _PoolNames.Clear();
        }
        
        public void Pop(string path, Action<GameObject> callback, bool autoActive = true)
        {
            _PoolNames.Add(path);
            GameObjectPoolManager.GetInstance().Pop(path, callback, autoActive);
        }

        public void Push(string path, GameObject go)
        {
            GameObjectPoolManager.GetInstance().Push(path, go);
        }

        public void Preload(string path, Action callback)
        {
            Pop(path, go =>
            {
                Push(path, go);
                callback?.Invoke();
            }, false);
        }

        public void CreateEffectPool(int configId, int initNum, int type, GameObject asset,Action<int> callBack = null)
        {
            if (effectPoolMap.ContainsKey(configId))
            {
                //Debug.LogError(LogModule.LibertyScene,"重复创建特效对象池：" + configId.ToString());
                return;
            }

            FrameExecuteTimeMonitor.AddTask(() =>
            {
                GameObject originObject = GameObject.Instantiate(asset);
                originObject.transform.parent = this.EffectRoot;
                originObject.SetActive(false);
                EffectPrefabPool effectPrefabPool = new EffectPrefabPool(initNum, initNum * 2, type, originObject);
                effectPoolMap[configId] = effectPrefabPool;
                effectPrefabPool.PreInit();
                callBack?.Invoke(configId);
            });
        }
        
        public Transform GetEffectObject(int configId,int uniqueId)
        {
            if (showingEffectMap.ContainsKey(uniqueId))
            {
                Debug.LogError(LogModule.LibertyScene,"创建特效失败！这个ID的特效已经在显示中了："+uniqueId.ToString());
                return null;
            }

            EffectPrefabPool prefabPool;
            if (!effectPoolMap.TryGetValue(configId,out prefabPool))
            {
                Debug.LogError(LogModule.LibertyScene,"创建特效失败！没有这个配置的特效："+configId.ToString());
                return null;
            }

            Transform effect = prefabPool.GetItem();
            showingEffectMap[uniqueId] = effect;
            unique2ConfigMap[uniqueId] = configId;
            return effect;
        }

        public void RecycleEffect(int uniqueId)
        {
            if (effectPoolMap == null)
            {
                return;
            }
            
            if (!showingEffectMap.ContainsKey(uniqueId))
            {
                //Debug.LogError(LogModule.LibertyScene,"移除特效失败，不存在这个唯一ID的特效："+uniqueId.ToString());
                return;
            }

            int configId = unique2ConfigMap[uniqueId];
            effectPoolMap[configId].RecoverItem(showingEffectMap[uniqueId]);

            showingEffectMap.Remove(uniqueId);
            unique2ConfigMap.Remove(uniqueId);
        }
        
        public Transform GetEffectObject(int configId)
        {
            EffectPrefabPool prefabPool;
            if (!effectPoolMap.TryGetValue(configId,out prefabPool))
            {
                Debug.LogError(LogModule.LibertyScene,"创建特效失败！没有这个配置的特效："+configId.ToString());
                return null;
            }

            Transform effect = prefabPool.GetItem();
            return effect;
        }
        
        public void RecycleEffect(int configId, Transform item, bool intoEffectRoot = true)
        {
            EffectPrefabPool pool = null;
            if (effectPoolMap.TryGetValue(configId, out pool))
            {
                pool.RecoverItem(item);
                if (intoEffectRoot)
                {
                    item.SetParent(EffectRoot);
                }
            }
            else
            {
                Destroy(item.gameObject);
            }
        }


        public override void Dispose()
        {
            foreach (var prefabPool in effectPoolMap)
            {
                prefabPool.Value.Destroy();
            }

            effectPoolMap = null;
            showingEffectMap = null;
            unique2ConfigMap = null;

            base.Dispose();
            
        }
        
    }
}